package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.model.RoleEquipField;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.utils.NotifyModules;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import com.kitty.game.equip.handler.UpgradeHandler;

@Component("EquipRefineApply")
public class EquipRefineApplyPreviewHandler extends UpgradeHandler {
    private static final String PINK = "粉色";
    private static final String GOLD = "金色";
    private static final String SUIT = "套装";

    private static final String SUCCESS_MESSAGE = "属性替换成功。";

    private static final List<Integer> PINKFIELD = Collections.singletonList(770);
    private static final List<Integer> PREPINKFIELD = Collections.singletonList(5890);

    private static final List<Integer> GOLDFIELD = Collections.singletonList(1026);
    private static final List<Integer> PREGOLDFIELD = Collections.singletonList(6146);

    private static final List<Integer> SUITFIELD = Arrays.asList(3074, 2050);
    private static final List<Integer> PRESUITFIELD = Arrays.asList(6402, 6658);

    @Override
    public String doUpgrade(Role role, Object... params) {
        return null;
    }

    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, upgradePos);
        if(roleEquip==null){
            return "请放入正确的装备。";
        }
        List<Integer> applyFields = new ArrayList<>();
        List<Integer> preFields = new ArrayList<>();
        switch (param) {
            case PINK:
                applyFields = PINKFIELD;
                preFields = PREPINKFIELD;
                break;
            case GOLD:
                applyFields = GOLDFIELD;
                preFields = PREGOLDFIELD;
                break;
            case SUIT:
                applyFields = SUITFIELD;
                preFields = PRESUITFIELD;
                break;
        }
        AtomicInteger i = new AtomicInteger();
        List<Integer> finalApplyFields = applyFields;
        preFields.forEach(preField -> {
            int p = i.getAndIncrement();
            RoleEquipField roleEquipField = roleEquip.getFields().get(preField.shortValue());
            roleEquipField.setType(finalApplyFields.get(p).shortValue());
            roleEquip.getFields().remove(preField.shortValue());
            roleEquip.getFields().put(roleEquipField.getType(), roleEquipField);
        });
        refreshRoleEquip(role, roleEquip);
        equipService.countPerfect(roleEquip);
        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setValue(roleEquip.getId() + "");
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_EQUIP_REFINE_OK);//炼化成功
        MessagePusher.pushMessage(role, respGeneralNotify);
        return SUCCESS_MESSAGE;
    }

    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    @Override
    public String getUpdateTypeStr() {
        return null;
    }
}
